package mobi.bantu.bean.task;

import jdk.nashorn.internal.ir.annotations.Ignore;
import mobi.bantu.bean.Cons;
import mobi.bantu.bean.Operations;
import mobi.bantu.bean.xls.ExcelData;
import mobi.bantu.utils.JianUtils;

import java.io.File;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.type.Alias;
import org.springframework.util.CollectionUtils;

import com.fasterxml.jackson.annotation.JsonFormat;

@Alias("report")
public class Report implements Serializable {
    private Long id;
    private Long userId;
    private String name;
    private String mobile;
    private String area; //区域
    private String project; //项目
    private String detail; //详情
    private String images;
    private Integer status;
    @JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm:ss")
    private Date createTime;
    private String evaluate; //评价
    private String remark;
    private Long mainId;

    List<ReportLog> logs;
    private Map<Operations, ReportLog> logMap;

    public Report() {
        this.status = Cons.INIT;
        this.createTime = new Date();
    }

    @ExcelData(title = "报修人", order = 6)
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getRemark() {
        return remark;
    }

    public void setRemark(String remark) {
        this.remark = remark;
    }

    @ExcelData(title = "报修人电话", order = 7)
    public String getMobile() {
        return mobile;
    }

    public void setMobile(String mobile) {
        this.mobile = mobile;
    }

    public String getImages() {
        return images;
    }

    public void setImages(String images) {
        this.images = images;
    }

    @ExcelData(title = "区域", order = 1)
    public String getArea() {
        return area;
    }

    public void setArea(String area) {
        this.area = area;
    }

    public Integer getStatus() {
        return status;
    }

    public void setStatus(Integer status) {
        this.status = status;
    }

    public Long getUserId() {
        return userId;
    }

    public void setUserId(Long userId) {
        this.userId = userId;
    }

    @ExcelData(title = "时间", order = 17)
    public Date getCreateTime() {
        return createTime;
    }

    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    @ExcelData(title = "审核人", order = 12)
    public String getSureInfo() {
        ReportLog log = getHanderLog(Operations.MANAGER_SURE);
        if (log == null) return "";
        return String.format("%s(%s)(%s)", log.getHanderName(), log.getHanderMobile(),log.getCreateTimeStr());
    }

    @ExcelData(title = "维修人", order = 13)
    public String getMainInfo() {
        ReportLog log = getHanderLog(Operations.MAINTAIN);
        if (log == null) return "";
        return String.format("%s(%s)(%s)", log.getHanderName(), log.getHanderMobile(),log.getCreateTimeStr());
    }

    @ExcelData(title = "办结人", order = 14)
    public String getOverInfo() {
        ReportLog log = getHanderLog(Operations.MAINTAIN_OVER);
        if (log == null) return "";
        return String.format("%s(%s)(%s)", log.getHanderName(), log.getHanderMobile(),log.getCreateTimeStr());
    }

    @Ignore
    private ReportLog getHanderLog(Operations op) {
        if (CollectionUtils.isEmpty(getLogs())) return null;
        return logMap.get(op);
    }

    public List<ReportLog> getLogs() {
        return logs;
    }

    public void setLogs(List<ReportLog> logs) {
        this.logs = logs;
        if (!CollectionUtils.isEmpty(logs)) {
            logMap = JianUtils.logsTransMap(logs);
        }
    }

    @ExcelData(title = "项目", order = 3)
    public String getProject() {
        return project;
    }

    public void setProject(String project) {
        this.project = project;
    }

    @ExcelData(title = "详情", order = 5)
    public String getDetail() {
        return detail;
    }

    public void setDetail(String detail) {
        this.detail = detail;
    }

    @ExcelData(title = "评价", order = 11)
    public String getEvaluate() {
        return evaluate;
    }

    public void setEvaluate(String evaluate) {
        this.evaluate = evaluate;
    }

    public Long getMainId() {
        return mainId;
    }

    public void setMainId(Long mainId) {
        this.mainId = mainId;
    }

    @ExcelData(title = "状态", order = 9)
    public String getStateDict() {
        if (this.status == null) return "待处理";
        switch (status) {
            case 0:
                return "等待管理员指定维修人员";
            case 1:
                return "已指定维修人员,待维修";
            case 2:
                return "维修完成," + (isEva() ? "已评价" : "请评价");
            case 9:
                return "已办结," + (isEva() ? "已评价" : "请评价");
        }
        return "未知";
    }

    public boolean isEva() {
        return StringUtils.isNotBlank(this.evaluate);
    }

    public boolean isOver() {
        return this.status == 9 || this.status == 2;
    }


    public List<String> getImgAry() {
        if (StringUtils.isBlank(this.images)) return Collections.EMPTY_LIST;
        List<String> result = new ArrayList<>();
        for (String name : this.images.split(",")) {
            result.add(Cons.FILE_ROOT + File.separator + name);
        }
        return result;
    }
}
